package com.ys.shop.factory;


import com.ys.shop.dao.IDAO;
import com.ys.shop.util.MyUtil;

import java.util.Map;

public class  DaoFactoryImp implements Ifactory{
    //饿汉式
    private static DaoFactoryImp daoFactoryImp=new DaoFactoryImp();
    private DaoFactoryImp(){};
    public static DaoFactoryImp getInstance(){
        return daoFactoryImp;
    }
    //根据参数创建DAO的方法
    @Override
    public IDAO createDAO(String type) throws Exception {
        Map<String,String> map= MyUtil.readXML("dao-config.xml");
        String daostring=map.get(type);
        IDAO dao=(IDAO) Class.forName(daostring).newInstance();
        return dao;
    }
    //测试
    public static void main(String[] args) {
        try {
            System.out.println(DaoFactoryImp.getInstance().createDAO("userdao").getAll().size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}